﻿using DevExpress.XtraBars;
using DevExpress.XtraGrid.Views.Base;
using NAGFwk.BL;
using NAGFwk.UI;
using SilkeyERP.BL;

namespace SilkeyERP.UI.Entities.Bank
{
    public partial class ContentView : BaseContentDetail
    {
        private IBaseController<Model.Bank> _controller = new BankController();

        public override string ModuleName { get { return "Bank"; } }

        public ContentView()
        {
            InitializeComponent();
        }

        public override void ShowModule(bool firstShow)
        {
            base.ShowModule(firstShow);
            MapItLoad(_controller);
            cityBindingSource.DataSource = new CityController().GetAll();       
        }

        private void bbiDelete_ItemClick(object sender, ItemClickEventArgs e)
        {
            MapItDelete(_controller);
        }

        protected override void ValidateRows(ValidateRowEventArgs e)
        {
            var typedRow = (Model.Bank)e.Row;
            if(string.IsNullOrEmpty(typedRow.BranchNumber))
            {
                e.ErrorText = "Numero de Sucursal es Requerido.";
                e.Valid = false;
            }
            if (string.IsNullOrEmpty(typedRow.ContactName))
            {
                e.ErrorText = "Nombre de Contacto es Requerido.";
                e.Valid = false;
            }
            if (string.IsNullOrEmpty(typedRow.ContactEmail))
            {
                e.ErrorText = "Email de Contacto es Requerido.";
                e.Valid = false;
            }
            if (string.IsNullOrEmpty(typedRow.ContactPhone))
            {
                e.ErrorText = "Telefono de Contacto es Requerido.";
                e.Valid = false;
            }
            if (string.IsNullOrEmpty(typedRow.Description))
            {
                e.ErrorText = "Descripcion es Requerido.";
                e.Valid = false;
            }
            if (typedRow.CityID == int.MinValue)
            {
                e.ErrorText = "Ciudad es Requerido.";
                e.Valid = false;
            }
        }

        private void RefreshAll()
        {
            _controller = MapItRefresh(new BankController());
            cityBindingSource.DataSource = new CityController().GetAll();              
        }

        private void bbiRefresh_ItemClick(object sender, ItemClickEventArgs e)
        {
            RefreshAll();
        }
    }
}
